package com.febers.ihealth.fragment

import com.febers.ihealth.R
import com.febers.ihealth.base.BaseFragment
import com.febers.ihealth.entity.ReleaseNextSurveyEvent
import com.febers.ihealth.utils.Event
import com.febers.ihealth.utils.user
import kotlinx.android.synthetic.main.survey_fu_xiaobian.*

class SurveyFuXiaobian: BaseFragment() {

    private var checkedCount = 0
        set(value) {
            field = value
            if (field > 0) {
                Event.post(ReleaseNextSurveyEvent(true))
            } else {
                Event.post(ReleaseNextSurveyEvent(false))
            }
        }

    override fun setView(): Int = R.layout.survey_fu_xiaobian

    override fun initView() {
        fun onCheckChange(isChecked: Boolean) {
            if (isChecked) {
                checkedCount++
                radio_button_wuzhengzhuang1.clearCheck()
            } else {
                checkedCount--
            }
        }
        //1
        radio_button_futong.setOnCheckedChangeListener { buttonView, isChecked ->
            user.futong = isChecked
            onCheckChange(isChecked)
        }
        radio_button_xiongxie_zhangman.setOnCheckedChangeListener { buttonView, isChecked ->
            user.xiongxiezhangman = isChecked
            onCheckChange(isChecked)
        }
        radio_button_xietong.setOnCheckedChangeListener { buttonView, isChecked ->
            user.xietong = isChecked
            onCheckChange(isChecked)
        }
        radio_button_xiongmen.setOnCheckedChangeListener { buttonView, isChecked ->
            user.xiongmen = isChecked
            onCheckChange(isChecked)
        }
        radio_button_touzhong.setOnCheckedChangeListener { buttonView, isChecked ->
            user.touzhong = isChecked
            onCheckChange(isChecked)
        }
        radio_button_shenzhong.setOnCheckedChangeListener { buttonView, isChecked ->
            user.shenzhong = isChecked
            onCheckChange(isChecked)
        }
        radio_button_juandai.setOnCheckedChangeListener { buttonView, isChecked ->
            user.juandai = isChecked
            onCheckChange(isChecked)
        }
        radio_button_xinfan.setOnCheckedChangeListener { buttonView, isChecked ->
            user.xinfan = isChecked
            onCheckChange(isChecked)
        }

        //2
        radio_button_bianhuang.setOnCheckedChangeListener { buttonView, isChecked ->
            user.niaohuang = isChecked
            if (isChecked) {
                radio_button_niaoqingchang.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_bianshao.setOnCheckedChangeListener { buttonView, isChecked ->
            user.niaoshao = isChecked
            if (isChecked) {
                radio_button_niaoqingchang.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_niaoqingchang.setOnCheckedChangeListener { buttonView, isChecked ->
            user.niaoqingchang = isChecked
            if (isChecked) {
                radio_button_bianhuang.clearCheck()
                radio_button_bianshao.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_yeniao.setOnCheckedChangeListener { buttonView, isChecked ->
            user.yeniaoduo = isChecked
            onCheckChange(isChecked)
        }

        //3
        radio_button_kouku.setOnCheckedChangeListener { buttonView, isChecked ->
            user.kouku = isChecked
            onCheckChange(isChecked)
        }
        radio_button_koubuke.setOnCheckedChangeListener { buttonView, isChecked ->
            user.koubuke = isChecked
            if (isChecked) {
                radio_button_koukeyinduo.clearCheck()
                radio_button_kougan.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_koukeyinduo.setOnCheckedChangeListener { buttonView, isChecked ->
            user.koukeyinduo = isChecked
            if (isChecked) {
                radio_button_koubuke.clearCheck()
                radio_button_kougan.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_kougan.setOnCheckedChangeListener { buttonView, isChecked ->
            user.kouganbuyuyin = isChecked
            if (isChecked) {
                radio_button_koubuke.clearCheck()
                radio_button_koukeyinduo.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_xilengyin.setOnCheckedChangeListener { buttonView, isChecked ->
            user.xilengyin = isChecked
            if (isChecked) {
                radio_button_xireyin.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_xireyin.setOnCheckedChangeListener { buttonView, isChecked ->
            user.xireyin = isChecked
            if (isChecked) {
                radio_button_xilengyin.clearCheck()
            }
            onCheckChange(isChecked)
        }

        //无
        radio_button_wuzhengzhuang1.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                checkedCount++
            } else {
                checkedCount--
            }
            radio_button_futong.clearCheck()
            radio_button_xiongxie_zhangman.clearCheck()
            radio_button_xietong.clearCheck()
            radio_button_xiongmen.clearCheck()
            radio_button_touzhong.clearCheck()
            radio_button_shenzhong.clearCheck()

            radio_button_bianhuang.clearCheck()
            radio_button_bianshao.clearCheck()
            radio_button_niaoqingchang.clearCheck()
            radio_button_yeniao.clearCheck()
            radio_button_kouku.clearCheck()
            radio_button_koubuke.clearCheck()
            radio_button_koukeyinduo.clearCheck()
            radio_button_kougan.clearCheck()
            radio_button_xilengyin.clearCheck()
            radio_button_xireyin.clearCheck()
        }
    }

    override fun lazyInit() {
    }

    override fun onVisible() {
        Event.post(ReleaseNextSurveyEvent(false))
        if (checkedCount > 0) {
            Event.post(ReleaseNextSurveyEvent(true))
        }
    }
}